#include <stdio.h>
#include <unistd.h> 
#include <arpa/inet.h>
#include <netdb.h> 
#include <stdlib.h>
#include <string.h>

#include <network.h> 
#include <server.h>


void connection2mainserver(){
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        exitNow("Ошибка при создании сокета.");
	
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(port);
    bcopy((char *)server->h_addr, (char *)&serveraddr.sin_addr.s_addr, server->h_length);

    if (connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0){
        exitNow("Ошибка при установке соединения."); 
	}
}


void sendBackup(int sockfd){
	int i;
	for(i = 0; i < MAXCONNECTION; i++){
		if(clients[i].sockfd < 0) continue;
		if(clients[i].sockfd == sockfd) continue;
		printf("SendBackup\n");
		reserveClient(clients[i].id, clients[i].name, clients[i].flag);
	}
	sending(sockfd, ENDBACKUP, NULL);
}

void cloning(){
	char *receiveMessage;
	int code;
	int i;
	char *temp;
	sleep(1);
    sending(sockfd, ALTSERVER, NULL);

    while((sockfd > 0) && ((recv(sockfd, &code, sizeof(int), MSG_PEEK)) > 0)){
  		receiveMessage = receiving(sockfd);
  		printf("Пришло %d\n", code);
 		switch(code){
			case CLIENTID:{
				for(i = 0; i < MAXCONNECTION; i++){
					if(clients[i].id != -1) continue;
					clients[i].id =  atoi(receiveMessage);
					if(clients[i].id > maxid) maxid = clients[i].id;
					clients[i].sockfd = -2;
					break;
				}	
				break;
			}
			case CLIENTNAME:{
				int id;
				temp = strchr(receiveMessage, 'N') + 1;  
				id =  strlen(receiveMessage) - strlen(temp) - 1; 
				receiveMessage[id] = '\0';  
				id = atoi(receiveMessage);    
				for(i = 0; i < MAXCONNECTION; i++){
					if(clients[i].id != id) continue;
					strcpy(clients[i].name, temp);
					break;
				}
					
				break;
			}
			
			case CLIENTFLAG:{
				int id, flag;
				temp = strchr(receiveMessage, 'F') + 1;  
				id =  strlen(receiveMessage) - strlen(temp) - 1; 
				receiveMessage[id] = '\0';  
				id = atoi(receiveMessage); 
				flag = atoi(temp);
				for(i = 0; i < MAXCONNECTION; i++){
					if(clients[i].id != id) continue;
					clients[i].flag =  atoi(receiveMessage);
					break;
				}
				break;
			}
			case CLIENTQUIT:{
				int id = atoi(receiveMessage);
				for(i = 0; i < MAXCONNECTION; i++){
					if(clients[i].id != id) continue;
					clients[i].id = -1;
					strcpy (clients[i].name, "Anonymouse");
					clients[i].flag = 0;
					break;
				}	
				break;
			}
		}
	}
}

void reserveFlag(int id, int flag){
	char buffer[9];
	sprintf(buffer, "%d%c%d", id, 'F', flag);
	sending(reserve, CLIENTFLAG, buffer);
}

void reserveName(int id, char name[12]){
	char buffer[17];
	sprintf(buffer, "%d%c%s", id, 'N', name);
	sending(reserve, CLIENTNAME, buffer);
}

void reserveClient(int id, char name[12], int flag){
	char buffer[4];
	sprintf(buffer, "%d", id);
	printf("id %d, name %s,  flag %d\n",id,name,flag);
	sending(reserve, CLIENTID, buffer);
	reserveName(id, name);
	reserveFlag(id, flag);
}


void reserveClientQuit(int id){
	char buffer[4];
	sprintf(buffer, "%d", id);
	sending(reserve, CLIENTQUIT, buffer);
}

void printAllInfo(){
	int i;
	printf("\n");
	for(i = 0; i < MAXCONNECTION; i++){
		printf("ID: %d\tName: %s\tFlag:%d\t",clients[i].id, clients[i].name, clients[i].flag);
		if (clients[i].sockfd != -1) 		printf("Online\n");
		else 		printf("Off\n");
	}
}

